package com.zzyl;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;

import java.util.Set;
import java.util.concurrent.TimeUnit;

@SpringBootTest
public class Test111 {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;



    @Test
    public void testString() {
        // 普通的set方法
        redisTemplate.opsForValue().set("name", "张三");
        System.out.println(redisTemplate.opsForValue().get("name"));
        // 设置带有过期时间的key
        redisTemplate.opsForValue().set("token", "123qweasd",20, TimeUnit.SECONDS);
        System.out.println(redisTemplate.opsForValue().get("token"));

        // setnx 当指定的键key不存在时,会将key的值设置为value，返回true，否则返回false(不能覆盖原有值)
        System.out.println(redisTemplate.opsForValue().setIfAbsent("lock", "09876", 5, TimeUnit.MINUTES));
        System.out.println(redisTemplate.opsForValue().setIfAbsent("lock", "34567", 5, TimeUnit.MINUTES));
    }

   @Test
    public void RedisTest(){
       SetOperations<String, String> op = redisTemplate.opsForSet();
       Long count = op.add("set", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
       System.out.println("count = " + count);
       Set<String> set = op.members("set");
       System.out.println("set = " + set);
       op.add("set1", "12", "11", "13", "14", "5", "6", "7", "8", "9", "10");
       Set<String> set1 = op.members("set1");

       Set<String> intersect = op.intersect("set", "set1");
       System.out.println("intersect = " + intersect);

       Set<String> union = op.union("set", "set1");
       System.out.println("union = " + union);

       Set<String> difference = op.difference("set", "set1");
       System.out.println("difference = " + difference);
   }

   @Test
   public void ZsetTest(){
       ZSetOperations<String, String> op = redisTemplate.opsForZSet();
       op.add("zset", "1", 1);
       op.add("zset", "2", 2);
       op.add("zset", "3", 3);
       op.add("zset", "4", 4);
       Set<String> zset = op.range("zset", 0, -1);
       System.out.println("zset = " + zset);
       Set<ZSetOperations.TypedTuple<String>> zset1 = op.rangeWithScores("zset", 0, -1);
       for (ZSetOperations.TypedTuple<String> stringTypedTuple : zset1) {
           System.out.println("stringTypedTuple = " + stringTypedTuple);
       }


   }



}
